<?php
namespace app\serveruser\traits;
use \app\manage\service\ApiBase;
/**
 * @author:      李颖
 * @DateTime:    2017-08-03 14:16:39
 * @Description: 给$data添加user_id后运行一个接口oneStepRun
 */
trait AddUserIdToRunOne{

	/*
	* userIdField: 获取到的user_id的值对应的变量名称
	* needVerify: 在该方法中是否对“根据token获取token的存放内容”进行验证权限
	*/
	protected function _addUserIdToRunOne(string $userIdField = 'id', array $data = [], string $path, string $scene = '', string $success = '1', string $validate = '', string $logic = '', bool $needVerify = true, bool $addAppId = false): array 
	{
		$service = new ApiBase($data['token']);
		if($addAppId){     //获取app_id
			$app_key = ApiBase::getAppKey();
			$appInfo = $service->oneStepRun(['app_key'=>$app_key], 'serveruser/auth/App/find', '', 1, $validate, $logic);
			if($appInfo['status'] != SUCCESS_STATUS){
	    		return $appInfo;
	    	}
	    	$data['app_id'] = $appInfo['res']['id'];
		}
		//根据token获取用户信息
		if($needVerify){  
        	$userInfo = $service->oneStepRun(['token'=>$data['token']], 'serveruser/auth/Token/hGetAll', '', 1, $validate, $logic);
		}else{     //验证权限后调用该方法
        	$sendData['query'] = json_encode([['path'=>'serveruser/auth/Token/hGetAll', 'param'=>['token'=>$data['token']]]]);
		    $userInfo =  ApiBase::sendDataOne(config('proxy_all_url'), $sendData);
		}
		if($userInfo['status'] != SUCCESS_STATUS){ return $userInfo; }
		//执行$path的接口方法
		$data[$userIdField] = $userInfo['res']['id'];
		return $service->oneStepRun($data, $path, $scene, $success, $validate, $logic);
	}


}
